public class Solution {
    public int romanToInt(String s) {
        int res = 0;
        if (s.contains("IV")) {
            res += 4;
            s = s.replace("IV", "");
        }

        if (s.contains("IX")) {
            res += 9;
            s = s.replace("IX", "");
        }

        if (s.contains("XL")) {
            res += 40;
            s = s.replace("XL", "");
        }

        if (s.contains("XC")) {
            res += 90;
            s = s.replace("XC", "");
        }

        if (s.contains("CD")) {
            res += 400;
            s = s.replace("CD", "");
        }

        if (s.contains("CM")) {
            res += 900;
            s = s.replace("CM", "");
        }

        for (int i = 0; i < s.length(); i++) {
            char tmp = s.charAt(i);
            res += getValue(tmp);
        }
        return res;
    }

    public int getValue(char str) {
        int value = 0;
        switch (str) {
            case 'I':
                value = 1;
                break;
            case 'V':
                value = 5;
                break;
            case 'X':
                value = 10;
                break;
            case 'L':
                value = 50;
                break;
            case 'C':
                value = 100;
                break;
            case 'D':
                value = 500;
                break;
            case 'M':
                value = 1000;
                break;
        }
        return value;
    }
}